<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .wrap {
            width: 1226px;
            margin: 50px auto;          
            position: relative;         
        }

        .preview {
            width: 400px;
            height: 400px;
            margin-right: 20px;
            position: relative;
        }

        .show {
            width: 200px;
            height: 200px;
            background-color: rgba(0, 0, 0, 0.4);
            position: absolute;
            top: 0;
            left: 0;
            display: none;

        }

        .preview-scale {
            width: 600px;
            height: 600px;
            overflow: hidden;
            position: absolute;
            left: 410px;
            top: 0;
            display: none;

        }

        .smallImg {
            width: 400px;
            height: 400px;

        }

        .bigImg {
            width: 1200px;
            height: 1200px;
            position: absolute;
            left: 0;
            top: 0;

        }
    </style>
    <script src="./jquery/jquery-3.6.0.min.js"></script>
</head>

<body>
    <div class="container">
        <div class="wrap">
            <div class="preview">
                <img src="./images/123.png" alt="" class="smallImg">
                <div class="show"></div>
            </div>
            <div class="preview-scale">
                <img src="./images/123.png" alt="" class="bigImg">
            </div>
        </div>
    </div>

</body>
<script>
  //    1计算出  放大镜跟随鼠标移动的坐标  计算 盒子移动的最大距离
  //    2 大图比小图的比例 和 大图盒子与放大镜比例要一致

    //  show移动最大距离

    var maxLeft = $('.preview').width() - $('.show').width();
    var maxTop = $('.preview').height() - $('.show').height();

    // 放大图片的比例

    var scale = $('.bigImg').width() / $('.smallImg').width();


    // 鼠标 移入 preview 盒子  事件
    $('.preview').mouseover(function (e) {

        // 移入显示 
        $('.preview-scale').css('display', 'block')
        $('.show').css('display', 'block')
        //  鼠标的位置 相对于 盒子 preview 
        var previewOffset = $('.preview').offset();   //  获取盒子相对于窗口的距离

        //  计算 鼠标在 盒子 preview 的坐标 
        var x = e.pageX - previewOffset.left - $('.show').width() / 2;
        var y = e.pageY - previewOffset.top - $('.show').height() / 2;

        // 边距的限定

        if (x <= 0) x = 0;
        if (x >= maxLeft) x = maxLeft;
        if (y <= 0) y = 0;
        if (y >= maxTop) y = maxTop;


        $('.show').css({ "left": x + 'px', "top": y + 'px' });
        $('.bigImg').css({ "left": -scale * x + 'px', "top": -scale * y + 'px' })


    })

    // 移出隐藏
    $('.preview').mouseout(function () {

        $('.preview-scale').css('display', 'none')
        $('.show').css('display', 'none')

    })

</script>

</html>